package com.yitiao.multithread.case01;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * <h3>线程组演示</h3>
 * <p>线程名: db-conn-thread-0, 所在线程组: 数据库连接线程组</p>
 * <p>线程名: db-conn-thread-1, 所在线程组: 数据库连接线程组</p>
 */
public class ThreadGroupDemo {
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static List<Thread> DbConnThread() {
        ThreadGroup dbConnThreadGroup = new ThreadGroup("数据库连接线程组");
        List<Thread> dbConnThreadList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            // 创建的时候指定线程组
            Thread t = new Thread(dbConnThreadGroup, new Runnable() {
                @Override
                public void run() {
                    System.out.println("线程名: " + Thread.currentThread().getName()
                            + ", 所在线程组: " + Thread.currentThread().getThreadGroup().getName());
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "db-conn-thread-" + i);
            t.start();
            dbConnThreadList.add(t);  // 用于启动所有线程
        }
        return dbConnThreadList;
    }

    @SneakyThrows
    public static void main(String[] args) {
        List<Thread> dbConnThreadList = DbConnThread();
//        List<Thread> httpReqThreadList = httpReqThread();
//        startThread(dbConnThreadList);
//        startThread(httpReqThreadList);
        Thread.sleep(3000000);
    }
}

